package service;

import dao.ArticleDao;
import entity.Article;
import entity.PageEntity;
import entity.User;

import java.util.List;

public class ArticleService {
    private ArticleDao articleDao = new ArticleDao();

    /**
     * 文章搜索+分页
     *
     * @param title
     * @param currPage
     * @return
     */
    public PageEntity<Article> findPage(String title, Integer currPage) {

        //创建一个页的实体对象
        PageEntity<Article> pageData = new PageEntity<>();
        Integer pageSize = pageData.getPageSize();

        //当前页的数据
        Integer startIndex = (currPage - 1) * pageSize;
        List<Article> rowData = articleDao.findPage(title, startIndex, pageSize);

        //总记录数
        Integer totalCount = articleDao.countByTitle(title);

        //总页数
        Integer totalPage = (totalCount % pageSize == 0) ? totalCount / pageSize : totalCount / pageSize + 1;

        pageData.setCurrPage(currPage);
        pageData.setData(rowData);
        pageData.setTotalCount(totalCount);
        pageData.setTotalPage(totalPage);

        return pageData;

    }

    /**
     * 发布文章
     *
     * @param article
     */
    public void save(Article article) {
        articleDao.save(article);

    }

    /**
     * 浏览量+1
     *
     * @param id
     */
    public void updateBrowseCount(Long id) {
        articleDao.updateBrowseCount(id);
    }

    /**
     * 查询文章详情
     *
     * @param id
     * @return
     */
    public Article findById(Long id) {
        return articleDao.findById(id);

    }

    /**
     * 文章的收藏数量
     *
     * @param id
     * @return
     */
    public Integer findFavoriteCount(Long id) {
        return articleDao.findFavoriteCount(id);
    }

    /**
     * 根据用户和文章查询是否有收藏关系
     *
     * @param userId
     * @param articleId
     * @return
     */
    public Boolean isFavorite(Long userId, Long articleId) {
        return articleDao.isFavorite(userId, articleId);
    }

    /**
     * 根据用户id和文章id查询用户记录
     *
     * @param userId
     * @param articleId
     * @return
     */
    public List<User> findFavoriteList(Long userId, Long articleId) {
        return articleDao.findFavoriteList(userId, articleId);
    }

    /**
     * 取消收藏
     *
     * @param userId
     * @param articleId
     */
    public void delFavorite(Long userId, Long articleId) {
        articleDao.delFavorite(userId, articleId);

    }

    /**
     * 收藏文章
     *
     * @param userId
     * @param articleId
     */
    public void addFavorite(Long userId, Long articleId) {
        articleDao.addFavorite(userId, articleId);

    }

    /**
     * 我的收藏+搜索+分页
     *
     * @param title
     * @param userId
     * @param currPage
     * @return
     */
    public PageEntity<Article> findFavoritePage(String title, Long userId, Integer currPage) {

        //创建一个分页实体对象
        PageEntity<Article> pageData = new PageEntity<>();
        Integer pageSize = pageData.getPageSize();

        //当前页的数据
        Integer startIndex = (currPage - 1) * pageSize;
        List<Article> rowData = articleDao.findFavoritePage(startIndex, pageSize, title, userId);

        //总记录数
        Integer totalCount = articleDao.findFavoriteCountByUserId(userId);

        //总页数
        Integer totalPage = (totalCount % pageSize == 0) ? totalCount / pageSize : totalCount / pageSize + 1;

        pageData.setCurrPage(currPage);
        pageData.setData(rowData);
        pageData.setTotalCount(totalCount);
        pageData.setTotalPage(totalPage);

        return pageData;
    }
}
